<?php
class Shop_CartController extends Zend_Controller_Action {

	public $skin = array(
		'display' => array(
			'layout' => 'shop_cart_display'
		)
	);
	
	/**
	 * @var array
	 */
	public $acl = array(
		'index' 	=> 'shop_cart_list',
		'add'  		=> 'shop_cart_add',
		'remove' 	=> 'shop_cart_remove'
	);

	/**
	 * @var array
	 */
	public $ajaxable = array(
		'index' => array('json'),
		'add' 	=> array('json'),
		'remove'=> array('json')
	);

	/**
	 * @var Shop_Model_Cart
	 */
	public $cart;
	
	public function init() {
		/* @var $ajaxContext Zend_Controller_Action_Helper_AjaxContext */
		$ajaxContext = $this->_helper->getHelper('AjaxContext');
		$ajaxContext->setAutoJsonSerialization(false);
        $ajaxContext->initContext();

        $this->cart = Shop_Model_Cart::getInstance();
	}

	public function indexAction() {
		$this->_forward('display');
	}

	/**
	 * Wyświetl zawartość koszyka
	 * @return void
	 */
	public function displayAction() {
		/* @var Zend_Controller_Request_Http */
		$rq = $this->getRequest();
		if (is_array($amount = $rq->getPost('amount'))) {
			foreach ($amount as $productId => $amount) {
				// modyfikuj liczebność produktów w koszyku
				// tylko dla prduktów już w nim istniejących
				// - gdyby $productId nie istniało w koszyku
				//   to zostal by dodany nowy produkt!
				if ($this->cart->hasProduct($productId)) {
					$this->cart->addProduct($productId, $amount);
				}
			}
		}
		
		$this->view->products 	= $this->cart->getProducts();
		$this->view->totalPrice = $this->cart->getTotalPrice();
	}

	/**
	 * @return void
	 */
	public function addAction() {
		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();
		$identyfication = $rq->getParam('alias', $rq->getParam('id'));

		$this->cart->addProduct($identyfication);

		$status   = $this->view->status   = $this->cart->getStatus();
		$messages = $this->view->messages = $this->cart->getMessages();

		$message = ($status === Shop_Model_Cart::SUCCESS)
			? 'Produkt <b>"%s"</b> został dodany do koszyka'
			: 'Produkt <b>"%s"</b> nie został dodany do koszyka';

		$message = sprintf($message,
						   $this->cart->getProductData($identyfication, 'name'));

		array_unshift($messages, $message);
		
		if (false === $rq->isXmlHttpRequest()) {
			/* @var $flash Zend_Controller_Action_Helper_FlashMessenger */
			$flashMessenger = $this->_helper->getHelper('FlashMessenger');
			array_map(array($flashMessenger,'addMessage'), $messages);
		
			/* @var $redirector Zend_Controller_Action_Helper_Redirector */
			$redirector = $this->_helper->getHelper('Redirector');
			$redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}
	}

	/**
	 * @return void
	 */
	public function removeAction() {
		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();
		$identyfication = $rq->getParam('alias', $rq->getParam('id'));

		$this->cart->removeProduct($identyfication);

		$status   = $this->view->status   = $this->cart->getStatus();
		$messages = $this->view->messages = $this->cart->getMessages();
		
		$message = 'Produkt <b>"%s"</b> został usunięty z koszyka';

		$message = sprintf($message,
						   $this->cart->getProductData($identyfication, 'name'));

		array_unshift($messages, $message);

		if (false === $rq->isXmlHttpRequest()) {
			/* @var $flash Zend_Controller_Action_Helper_FlashMessenger */
			$flashMessenger = $this->_helper->getHelper('FlashMessenger');
			array_map(array($flashMessenger,'addMessage'), $messages);

			/* @var $redirector Zend_Controller_Action_Helper_Redirector */
			$redirector = $this->_helper->getHelper('Redirector');
			$redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}
	}
}